<template>
  <div>
      <Header></Header>
    <div class="nav">
      <div class="city_tip">
        <span>当前定位城市：</span>
        <span>定位不准时，请在城市列表中选择</span>
      </div>
      <div class="guess_city">
        <span @click="city(guess)">{{ guess.name }}</span>
        <span>></span>
      </div>
    </div>
    <section id="hot_city_container">
      <h4>热门城市</h4>
      <ul>
        <li v-for="A in hot" :key="A.id" @click="city(A)">{{ A.name }}</li>
      </ul>
    </section>
    <section class="group_city_container">
      <ul class="letter_classify">
        <li
          class="letter_classify_li"
          v-for="(A, i, index) in group"
          :key="A.id"
        >
          <h4 class="city_title">
            {{ i }}
            <span v-if="index == 0">(按字母排序)</span>
          </h4>
          <ul class="groupcity_name_container citylistul">
            <li class="ellipsis" v-for="B in A" @click="city(B)" :key="B.id">{{ B.name }}</li>
          </ul>
        </li>
      </ul>
    </section>
  </div>
</template>

<script>
import Header from '../common/header.vue'
export default {
  name: "ElmIndex",
  components:{
    Header
  },
  data() {
    return {
      guess: [],
      hot: [],
      group: [],
    };
  },
  methods: {
    city(A){
        console.log(A);
        this.$router.push({path:'/city',query:{id:A.id}})
        // localStorage.setItem('cityObj',JSON.stringify(A))
        this.$store.commit('cityObj',A);
    }
  },

  mounted() {
    this.axios.get("http://elm.cangdu.org/v1/cities?type=guess").then((res) => {
      if (res.data) {
        this.guess = res.data;
      }
    });
    this.axios.get("http://elm.cangdu.org/v1/cities?type=hot").then((res) => {
      if (res.data) {
        this.hot = res.data;
      }
    });
    this.axios.get("http://elm.cangdu.org/v1/cities?type=group").then((res) => {
      if (res.data) {
        let sortobj = {};
        for (let i = 65; i <= 90; i++) {
          if (res.data[String.fromCharCode(i)]) {
            sortobj[String.fromCharCode(i)] = res.data[String.fromCharCode(i)];
          }
        }
        this.group = sortobj;
      }
    });
  },

};
</script>

<style scoped lang='scss'>

.nav {
  padding-top: 2.35rem;
  border-top: 1px solid #e4e4e4;
  background-color: #fff;
  margin-bottom: 0.4rem;
  .city_tip {
    display: flex;
    -ms-flex-pack: justify;
    justify-content: space-between;
    line-height: 1.45rem;
    padding: 0 0.45rem;
    span {
      &:nth-of-type(1) {
        font-size: 0.55rem;
        color: #666;
      }
      &:nth-of-type(2) {
        font-weight: 900;
        font-size: 0.475rem;
        color: #9f9f9f;
      }
    }
  }
  .guess_city {
    display: -ms-flexbox;
    display: flex;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -ms-flex-align: center;
    align-items: center;
    height: 1.8rem;
    padding: 0 0.45rem;
    border-top: 1px solid #e4e4e4;
    border-bottom: 2px solid #e4e4e4;
    font: 0.75rem/1.8rem Microsoft YaHei;
    span {
      &:nth-of-type(1) {
        color: #3190e8;
      }
    }
  }
}
#hot_city_container {
  background-color: #fff;
  margin-bottom: 0.4rem;
  h4 {
    color: #666;
    font-weight: 400;
    text-indent: 0.45rem;
    border-top: 2px solid #e4e4e4;
    border-bottom: 1px solid #e4e4e4;
    font: 0.55rem/1.45rem Helvetica Neue;
    text-align: left;
  }
  ul {
    zoom: 1;
    display: flex;
    flex-wrap: wrap;
    li {
      text-align: center;
      color: #3190e8;
      border-bottom: 0.025rem solid #e4e4e4;
      border-right: 0.025rem solid #e4e4e4;
      width: 25%;
      height: 1.75rem;
      font: 0.6rem/1.75rem Microsoft YaHei;
    }
  }
}
.group_city_container {
  .letter_classify {
    .letter_classify_li {
      margin-bottom: 0.4rem;
      background-color: #fff;
      border-bottom: 1px solid #e4e4e4;
      .city_title {
        color: #666;
        font-weight: 400;
        text-indent: 0.45rem;
        border-top: 2px solid #e4e4e4;
        border-bottom: 1px solid #e4e4e4;
        font: 0.55rem/1.45rem Helvetica Neue;
        text-align: left;
        span {
          font-size: 0.475rem;
          color: #999;
        }
      }
      .groupcity_name_container {
          display: flex;
          flex-wrap: wrap;
        .ellipsis {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          color: #666 !important;
          text-align: center;
          color: #3190e8;
          border-bottom: 0.025rem solid #e4e4e4;
          border-right: 0.025rem solid #e4e4e4;
          width: 25%;
          height: 1.75rem;
          font: 0.6rem/1.75rem Microsoft YaHei;
        }
      }
    }
  }
}
</style>